﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;
using WindowsForms.Class;
using WindowsForms.service;

namespace WindowsForms
{
    public partial class MenuTypeManagement : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        List<MenuTypes> lsMenuType;
        public MenuTypeManagement()
        {
            iQuery = new Service1();
            InitializeComponent();
            GetAllMenuType();
        }
        private void GetAllMenuType()
        {
            ConvertToList convert = new ConvertToList();
            lsMenuType = convert.ConvertTolistMenuTypes(iQuery.SelectMenuTypesAll().Tables[0]);
            gridControlMenuType.DataSource = lsMenuType;
            btnEdit.Enabled = false;
            btnDel.Enabled = false;
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (MenuTypeProcess frmMenuType = new MenuTypeProcess())
            {
                if (frmMenuType.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    MenuTypes menuTypeObj = new MenuTypes();
                    menuTypeObj.MenuTypeName = frmMenuType.MT_Name;
                    menuTypeObj.Description = frmMenuType.MT_Des;
                    
                    iQuery.InsertMenuType(menuTypeObj.MenuTypeName, menuTypeObj.Description);
                    lsMenuType.Add(menuTypeObj);
                    gvMenuType.RefreshData();
                }
            }
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvMenuType.GetSelectedRows();
            MenuTypes menuTypeSelected = gvMenuType.GetRow(rows[rows.Length - 1]) as MenuTypes;

            using (MenuTypeProcess frmMenu = new MenuTypeProcess(true))
            {
                frmMenu.MT_Name = menuTypeSelected.MenuTypeName;
                frmMenu.MT_Des = menuTypeSelected.Description;

                if (frmMenu.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    menuTypeSelected.MenuTypeName = frmMenu.MT_Name;
                    menuTypeSelected.Description = frmMenu.MT_Des;
                    iQuery.UpdateMenuType(menuTypeSelected.MenuTypeID, menuTypeSelected.MenuTypeName, menuTypeSelected.Description, menuTypeSelected.IsDeleted);
                    gvMenuType.RefreshData();
                }
            }
        }

        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvMenuType.GetSelectedRows();
            MenuTypes menuTypeSelected = gvMenuType.GetRow(rows[rows.Length - 1]) as MenuTypes;
            string message = string.Format(Constants.MESS_CONFIRMDELETE, menuTypeSelected.MenuTypeName);
            DialogResult dr = MessageBox.Show(message, Constants.MESS_DELETE_CAPTION, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                iQuery.DeleteMenuType(menuTypeSelected.MenuTypeID);
                lsMenuType.Remove(menuTypeSelected);
                gvMenuType.RefreshData();
            }
        }

        private void gvMenuType_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            btnEdit.Enabled = gvMenuType.SelectedRowsCount > 0;
            btnDel.Enabled = gvMenuType.SelectedRowsCount > 0;
        }

        private void MenuTypeManagement_Load(object sender, EventArgs e)
        {

        }

    }
}